package com.keshe.service;


import java.util.Date;
import java.util.List;

import com.keshe.dao.RoomDao;
import com.keshe.dao.RoomInDao;
import com.keshe.domain.Room;
import com.keshe.domain.RoomIn;
import com.keshe.util.DateUtils;


public class RoomInContorller {


    //获取时间
    public RoomInContorller() {
    }

    public String getTime() {
        Date d = new Date();
        String time = DateUtils.dateToString(d, "yyyy-MM-dd HH:mm:ss");
        return time;
    }

    //显示入住信息至table
    public String[][] showALL() {
        RoomInDao ri = new RoomInDao();
        List<RoomIn> allRoomIn = ri.select();
        String table[][] = new String[allRoomIn.size()][7];
        for (int i = 0; i < allRoomIn.size(); i++) {
            table[i][1] = allRoomIn.get(i).getUsername();
            table[i][2] = allRoomIn.get(i).getIdcard();
            table[i][0] = allRoomIn.get(i).getRoomId();
            table[i][3] = allRoomIn.get(i).getDatain();
            table[i][4] = allRoomIn.get(i).getDataout();
            table[i][5] = Integer.toString(allRoomIn.get(i).getDataNum());
            table[i][6]=Integer.toString(allRoomIn.get(i).getCoastY());
        }
        return table;
    }

    //新旅客入住信息输入
    public int insertRoomIn(String roomid, String username, String idcard, int dataNum,int coastY) {
        int count = 0;
        if (roomid.equals("") || username.equals("") || idcard.equals("") || dataNum == 0) {
            count = -1;
        }
        //在客房表中查询是否有该客房
        RoomDao rd = new RoomDao();
        List<Room> room = rd.select();
        for (int i = 0; i < room.size(); i++) {
            if (room.get(i).getRoomid().equals(roomid)) {
                if (room.get(i).getHousesnull().equals("是")) {
                    String datain = getTime();
                    String dataout = "暂未退房";
                    RoomIn r = new RoomIn(username, idcard, roomid, datain, dataout, dataNum,coastY);
                    RoomInDao ri = new RoomInDao();
                    count = ri.insert(r);//调用数据库
                    if(count !=0){
                        int test = rd.updateId(roomid, "否");
                        if (test != 0) {
                            System.out.println("修改客房状态成功！");
                        }
                    }
                    return count;
                }else{
                    System.out.println("该客房已被占用！");
                    return -2;
                }
            }
        }
        System.out.println("客房中无该客房信息，不可入住");
        return count;
    }

    //旅客退房
    public int updateRoomInNull(String idcard) {
        RoomInDao ri = new RoomInDao();
        List<RoomIn> allRoomIn = ri.select();
        int count = 0;
        if (idcard.equals("")) {
            System.out.println("输入数据后再搜索！");
            return -1;
        }
        for (int i = 0; i < allRoomIn.size(); i++) {
            if ((allRoomIn.get(i).getIdcard()).equals(idcard)) {
                String dateout = getTime();
                count = ri.updateDateout(dateout, idcard);
                RoomDao rd = new RoomDao();
                int test = rd.updateId(allRoomIn.get(0).getRoomId(), "是");
                if (test != 0) {
                    System.out.println("修改客房状态成功！");
                } else {
                    System.out.println("修改客房状态失败");
                }
                return count;//退房办理成功
            }
        }
        return count;
    }
}
